/*
 * @Author: zhangyang
 * @Date: 2021-07-06 15:08:23
 * @LastEditTime: 2022-01-13 19:14:47
 * @Description: 配合表格组件的导出方法
 */
import { export_json_to_excel } from './export2excel';

/**
 * 提取HTML标签包含的文字
 * @param {string} htmlText 包含 HTML 标签的字符串
 */
const tagReplace = (htmlText: string) => {
  let reg = /<\/?.+?\/?>/g;
  if (reg.test(htmlText)) {
    return htmlText.replace(reg, '');
  } else {
    return htmlText;
  }
};

/**
 * 导出数据格式化
 * @param {Array} filterVal 表头字段（用于获取对应的数据）
 * @param {Array} jsonData 表数据
 */
const formatJson = (filterVal: any[], jsonData: any[]): any[] => {
  return jsonData.map((v) => {
    return filterVal.map((j) => tagReplace(v[j]));
  });
};
interface ExportParams {
  filename: string;
  tableHead: any[];
  tableData: any[];
};
export const youngExport = ({ filename, tableHead, tableData }: ExportParams, sumText?: string, operate?: (txt: string, data: any[]) => any[]) => {
  const header = tableHead.map((item) => item.label);
  const filter = tableHead.map((item) => item.prop);
  let data = formatJson(filter, tableData);
  export_json_to_excel({
    header,
    data,
    filename
  });
};
